package com.bos.web.action;

import com.bos.domian.base.FixedArea;
import com.bos.service.FixedAreaSevice;
import com.bos.service.TakeTimeService;
import com.bos.web.BaseAction;
import com.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.cglib.core.Local;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.util.Collection;
import java.util.Map;

/**
 * @description:定区
 */
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)//多例的
public class FixedAreaAction extends BaseAction<FixedArea> {

    @Autowired
    private FixedAreaSevice fixedAreaSevice;

    @Action(value = "fixedArea_save", results = {@Result(location = "/pages/base/fixed_area.html"), @Result(name = "none", type = "redirect", location = "http://192.168.238.1:8081/error.html")})
    public String fixedAreaSave() {
        try {
            model.setId(model.getId());
            fixedAreaSevice.save(model);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return NONE;
        }
    }

    @Action(value = "fixedArea_pageQuery", results = {@Result(type = "json")})
    public String fixedAreaPageQuery() {
        Map<String, Object> fixedMap = fixedAreaSevice.pageQuery(page - 1, rows, model);
        ActionContext.getContext().getValueStack().push(fixedMap);
        return SUCCESS;
    }

    @Action(value = "noassociationCustomer", results = {@Result(type = "json")})
    public String noassociationCustomer() {
        //获取没有任何关联的客户信息,选择远程调用的方式获取
        Collection<? extends Customer> collection =
                WebClient.create("http://localhost:8082/services/customerService/notCustomerConFiexdArea")
                        //接收的参数格式                     //传递参数以什么格式传递
                        .accept(MediaType.APPLICATION_JSON)//.type(MediaType.APPLICATION_XML)
                        .encoding("utf-8")
                        .getCollection(Customer.class);
        ActionContext.getContext().getValueStack().push(collection);
        return SUCCESS;
    }

    @Action(value = "associationCustomer", results = {@Result(type = "json")})
    public String associationCustomer() {

        //获取与该地区绑定过的客户信息

        Collection<? extends Customer> collection = WebClient.create("http://localhost:8082/services/customerService/conCustomerAndFiexdArea/" + model.getId())
                .type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .encoding("utf-8")
                .getCollection(Customer.class);
        ActionContext.getContext().getValueStack().push(collection);

        return SUCCESS;
    }
    private String customerIds;



    public void setCustomerIds(String customerIds) {
        this.customerIds = customerIds;
    }

    @Action(value = "moveCustomerAndFixedArea",results = {@Result(location = "/pages/base/fixed_area.html")})
    public String moveCustomerAndFixedArea(){

       WebClient.create("http://localhost:8082/services/customerService/upCustomerOrFiexdArea/"+customerIds+"/"+model.getId())
               .put(null);

        return SUCCESS;
    }
  //分区关联快递员   快递员的收派时间操作.
   private Integer courierId ;
   private Integer takeTimeId ;

    public void setCourierId(Integer courierId) {
        this.courierId = courierId;
    }

    public void setTakeTimeId(Integer takeTimeId) {
        this.takeTimeId = takeTimeId;
    }

    @Action(value = "fixedArea_associationCourierToFixedArea",results = {@Result(location = "/pages/base/fixed_area.html")})
    public String associationCourierToFixedArea(){
        fixedAreaSevice.associationCourierToFixedArea(model.getId(),courierId,takeTimeId);
        return SUCCESS;
    }

}
